package com.zretc.support.controller;

import com.zretc.support.entity.Joincourse;
import com.zretc.support.service.JoincourseService;
import com.zretc.support.tool.PageData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/joincourse")
public class JoincourseController {

    @Autowired
    JoincourseService joincourseService;

    @GetMapping("listjoincoursebystu")
    @ApiOperation(value = "根据学生姓名参课分页查询",notes = "分页")
    public PageData<Joincourse> listJioncourseByStu(@RequestParam(name = "keywords", defaultValue = "",required = false) String keywords, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {

        Page<Joincourse> pg = joincourseService.getJoincourseByPageByStu(keywords, page, limit);
        PageData<Joincourse> pd = new PageData<Joincourse>();
        pd.setCode(0);
        pd.setRecords(pg.getContent());
        pd.setTotal((int) pg.getTotalElements());
        pd.setSize(pg.getSize());
        pd.setMsg("");
        pd.setCurrent(pg.getNumber() + 1);
        return pd;

    }

    @GetMapping("listjoincoursebycla")
    @ApiOperation(value = "根据课程名称参课分页查询",notes = "分页")
    public PageData<Joincourse> listJioncourseByCla(@RequestParam(name = "keywords", defaultValue = "",required = false) String keywords, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {

        Page<Joincourse> pg = joincourseService.getJoincourseByPageByCla(keywords, page, limit);
        PageData<Joincourse> pd = new PageData<Joincourse>();
        pd.setCode(0);
        pd.setRecords(pg.getContent());
        pd.setTotal((int) pg.getTotalElements());
        pd.setSize(pg.getSize());
        pd.setMsg("");
        pd.setCurrent(pg.getNumber() + 1);
        return pd;
    }

    @GetMapping("listjoincoursebyteacherbystu")
    @ApiOperation(value = "老师查询参课根据学生名称",notes = "分页")
    public PageData<Joincourse> listJioncourseByTeacherByStu(@RequestParam(name = "userId", defaultValue = "001",required = false) String userId,@RequestParam(name = "keywords", defaultValue = "",required = false) String keywords, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {

        Page<Joincourse> pg = joincourseService.getJoincourseByTeacherByStu(userId, keywords, page, limit);
        PageData<Joincourse> pd = new PageData<Joincourse>();
        pd.setCode(0);
        pd.setRecords(pg.getContent());
        pd.setTotal((int) pg.getTotalElements());
        pd.setSize(pg.getSize());
        pd.setMsg("");
        pd.setCurrent(pg.getNumber() + 1);
        return pd;

    }

    @GetMapping("listjoincoursebyteacherbycla")
    @ApiOperation(value = "老师查询参课根据课程名称",notes = "分页")
    public PageData<Joincourse> listJioncourseByTeacherByCla(@RequestParam(name = "userId", defaultValue = "001",required = false) String userId,@RequestParam(name = "keywords", defaultValue = "",required = false) String keywords, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {

        Page<Joincourse> pg = joincourseService.getJoincourseByTeacherByCla(userId, keywords, page, limit);
        PageData<Joincourse> pd = new PageData<Joincourse>();
        pd.setCode(0);
        pd.setRecords(pg.getContent());
        pd.setTotal((int) pg.getTotalElements());
        pd.setSize(pg.getSize());
        pd.setMsg("");
        pd.setCurrent(pg.getNumber() + 1);
        return pd;

    }

    @GetMapping("listjoincoursebystudent")
    @ApiOperation(value = "学生查询参课根据课程名称",notes = "分页")
    public PageData<Joincourse> listJioncourseByStudent(@RequestParam(name = "userId", defaultValue = "002",required = false) String userId,@RequestParam(name = "keywords", defaultValue = "",required = false) String keywords, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {

        Page<Joincourse> pg = joincourseService.getJoincourseByStudent(userId,keywords, page, limit);
        PageData<Joincourse> pd = new PageData<Joincourse>();
        pd.setCode(0);
        pd.setRecords(pg.getContent());
        pd.setTotal((int) pg.getTotalElements());
        pd.setSize(pg.getSize());
        pd.setMsg("");
        pd.setCurrent(pg.getNumber() + 1);
        return pd;

    }

    @PostMapping("addjoincourse")
    @ApiOperation(value = "参课添加")
    public Joincourse addJoincourse(@RequestBody Joincourse joincourse){
        return joincourseService.addJoincourse(joincourse);
    }

    @GetMapping("deletejoincourse/{joinId}")
    @ApiOperation(value = "参课删除")
    public String deleteJoincourse(@PathVariable("joinId") Long joinId){
        joincourseService.deleteJoincourse(joinId);
        return "删除成功";
    }
}
